package com.vdimitr.happycube.puzzle.solution.output.writer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * This class exports a puzzle piece solution to a file under a specified
 * folder. The name of the exported file will contain the provided description
 * augmented with pre- and post- fixes plus the number count for the solution.
 * 
 * @author vasileios.dimitriadis
 * 
 */
public class PuzzlePieceSolutionFileWriter extends
		AbstractPuzzlePieceSolutionWriter {

	/**
	 * The prefix of the file name to use for writing the puzzle piece solution
	 */
	public static final String outputFileNamePrefix = "_solution_";

	/**
	 * The postfix of the file name to use for writing the puzzle piece solution
	 */
	public static final String outputFileNamePostfix = ".txt";

	/**
	 * The folder to use for writing the files
	 */
	private File outputFolder = null;

	/**
	 * A counter of how many files have been written
	 */
	private int outputFileCounter = 1;

	/**
	 * Used for creating the file name of the files
	 */
	private String description = null;

	/**
	 * Instances of this class need to know the folder under which to produce
	 * the file containing the solution and a description to add at the file
	 * name
	 * 
	 * @param outputFolderName
	 *            a path to a folder to write the solutions to
	 * @param description
	 *            added to the file name of the generated file
	 */
	public PuzzlePieceSolutionFileWriter(String outputFolderName,
			String description) {
		this.outputFolder = new File(outputFolderName);
		this.description = description;
	}

	@Override
	protected OutputStream getOutputStream() throws IOException {
		String outputFileName = this.description + outputFileNamePrefix
				+ outputFileCounter + outputFileNamePostfix;
		File outputFile = new File(outputFolder, outputFileName);
		outputFileCounter++;
		return new FileOutputStream(outputFile);
	}

}
